<?php
/* ******************************
	TABLE aJax actions
   ******************************
*/
	include('../../includes/classes/core.php');
	$action 	= $_REQUEST['act'];
	$user_id	= $_COOKIE['USERID'];
	$error		= '';
	$output 	= '';
	
	switch ($action) {
		case 'get_add_page':
			$page			= GetPage();
			$output 		= array('page'	=> $page);		
			break;
		case 'get_edit_page':
			$object_id		= $_REQUEST['id'];
			$page			= GetPage(GetObb($object_id));		
			$output 		= array('page'	=> $page);		
			break;
	    case 'get_list':
    	    $count = $_REQUEST['count'];
    	    $hidden	= $_REQUEST['hidden'];
    	    $rResult = mysql_query("SELECT 		`tables`.id,
												`tables`.`name`,
												`object`.`name`
									FROM 		`tables`
									LEFT JOIN 	`object` ON `object`.`id` = `tables`.`object_id`
									WHERE   	`tables`.`actived` = 1");   			                            
			
			$output = array(
				"aaData"	=> array()
			);
			
			while ( $aRow = mysql_fetch_array( $rResult ) )
			{
				$row = array();
				for ( $i = 0 ; $i < $count ; $i++ )
				{
					/* General output */
					$row[] = $aRow[$i];
					if($i == ($count - 1)){
						$row[] = '<input type="checkbox" name="check_' . $aRow[$hidden] . '" class="check" value="' . $aRow[$hidden] . '" />';
					}
				}
				$output['aaData'][] = $row;
			}

	        break;
	        case 'save_object':
	        	
				$object_id		= $_REQUEST['id'];
				$name 		    = $_REQUEST['name'];
	        	$object			= $_REQUEST['object'];
	        	
	        	if($object_id ==''){
	        		AddObject($user_id, $name, $object);	        			
	        	}else{
	        		SaveObject($user_id, $object_id, $name, $object);	        		
	        	}

	        	break;
	        case 'disable':   
	        	$object_id = $_REQUEST['id'];
	        	Disable($object_id);
	        	break;
	    default:
	       	$error = 'Action is Null';
	}
	
	
	$output['error'] = $error;	
	echo json_encode($output);
	
	function GetObb($object_id)
	{
		$res = mysql_fetch_assoc(mysql_query("	SELECT	`tables`.`id`,
														`tables`.`name`,
														`tables`.`object_id`
												FROM	`tables`
												WHERE   `tables`.`actived` = 1 && `id`=$object_id"));
		return $res;
	}
	
	
	function AddObject($user_id, $name, $object)
	{
			mysql_query("INSERT INTO `tables`
									(`user_id`, `name`, `object_id`)
						VALUES
									($user_id, '$name', $object)");			
	
		
	}
	
	function SaveObject($user_id, $object_id, $name, $object)
	{

			mysql_query("UPDATE
								`tables`
						SET
								`user_id`	= $user_id,
								`name`		= '$name',
								`object`	= $object
						WHERE
								`id`		= $object_id");
						
	}
	
	
	function Disable($object_id)
	{
		mysql_query("UPDATE `tables`
					 SET    `actived` = 0
					 WHERE 	`id` = $object_id");
	}
	
	function GetobType($object_id){

		$req = mysql_query("SELECT  `object`.`name`
							FROM    `object`
							WHERE   `id`= $object_id");
		if( $req  ){
			return mysql_result($req ,0,'name');
		}
		return '';
	}
	
	function GetobjectType($object_id){
		$data = '';
		$req = mysql_query("SELECT	 		`object`.`id`,
       										`object`.`name`
							FROM   			`object`
							WHERE			`object`.type = 8");
		
		if($object_id == ''){
			$data = '<option value="0" selected="selected"></option>';
		}
		
		while( $res = mysql_fetch_assoc( $req )){
			if($res['id'] == GetobType($object_id)){
				$data .= '<option value="' . $res['id'] . '" selected="selected">' . $res['name'] . '</option>';
			} else {
				$data .= '<option value="' . $res['id'] . '">' . $res['name'] . '</option>';
			}
		}		
		return $data;		
	}
	
	function GetPage($res = '')
	{
		$data = '
			<div id="dialog-form">
			    <fieldset>
			    	<legend>ძირითადი ინფორმაცია</legend>
			
			    	<table class="dialog-form-table">
						<tr>
							<td style="width: 170px;"><label for="object_name">სახელი</label></td>
							<td>
								<input type="text" id="table_name" class="idle seoy-large" onblur="this.className=\'idle seoy-large\'" onfocus="this.className=\'activeField seoy-large\'" value="' . $res['name'] . '" />
							</td>
						</tr>
						<tr>
							<td style="width: 170px;"><label for="type">ობიექტი</label></td>
							<td>
								<select id="object" class="idls">' . GetobjectType($res['id']) . '</select>
							</td>
						</tr>												
					</table>
					<!-- ID -->
					<input type="hidden" id="object_id" value="'.$res['id']. '" />
		        </fieldset>
		    </div>
	    ';
		return $data;
	}
	
?>